package com.xps.charmingshop.view.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.charmingshop.R;
import com.xps.charmingshop.bean.LoginBean;
import com.xps.charmingshop.date.net.UserNetWorks;
import com.xps.charmingshop.utils.SharedPreferencesUtils;
import com.xps.charmingshop.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/1/22.
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.Cell_phone_number)
    EditText CellPhoneNumber;
    @BindView(R.id.Login_Password)
    EditText LoginPassword;
    @BindView(R.id.Sign_in)
    TextView SignIn;
    @BindView(R.id.Login_Register)
    TextView LoginRegister;
    @BindView(R.id.Forgot_Password)
    TextView ForgotPassword;
    private static final String PREFS_NAME = "MyUserInfo";
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.iv_titleicon)
    ImageView ivTitleicon;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.actionbar_tv_right)
    TextView actionbarTvRight;
    @BindView(R.id.Remember_password)
    CheckBox RememberPassword;

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void setData() {
        actionbarIvBack.setVisibility(View.GONE);
        ivTitleicon.setVisibility(View.GONE);
        actionbarTvTitle.setText("登录");
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        LoadUserDate();
        Intent i = getIntent();
        if (i.getStringExtra("zhanghao") != null) {
            if (i.getStringExtra("zhanghao").equals("1")) {
                showCustomize();
            }
        }
    }

    @OnClick({R.id.Sign_in, R.id.Login_Register, R.id.Forgot_Password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.xieye_Text:
//                JUtils.Toast("正在开发");
//                break;
            case R.id.Sign_in:
                getLogin();
                SaveUserDate();
                showDialogNoCancel();
                break;
            case R.id.Login_Register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.Forgot_Password:
                JUtils.Toast("正在开发");
                break;
        }
    }

    public void getLogin() {
        UserNetWorks.getLogin(CellPhoneNumber.getText().toString(), LoginPassword.getText().toString(), new Subscriber<LoginBean>() {
            @Override
            public void onCompleted() {

            }


            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                dissmisDialog();
            }

            @Override
            public void onNext(LoginBean loginBean) {
                if (loginBean.getReturn_code().equals("SUCCESS")) {
                    dissmisDialog();
                    finish();
                    SharedPreferencesUtils.putString("user_token", "String", loginBean.getReturn_body().getShop_token());
                    Log.i("user_token", loginBean.getReturn_body().getShop_token());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    JUtils.Toast(loginBean.getReturn_msg());
                } else if (loginBean.getReturn_code().equals("FAIL")) {
                    dissmisDialog();
                    JUtils.Toast(loginBean.getReturn_msg());
                }
            }
        });
    }

    private void SaveUserDate() {
        // 载入配置文件
        SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
        // 写入配置文件
        SharedPreferences.Editor spEd = sp.edit();
        if (RememberPassword.isChecked()) {
            SharedPreferencesUtils.putBoolean("isSave", "boolean", true);
            spEd.putString("name", CellPhoneNumber.getText().toString());
            spEd.putString("password", LoginPassword.getText().toString());
        } else {
            SharedPreferencesUtils.putBoolean("isSave", "boolean", false);
            spEd.putString("name", "");
            spEd.putString("password", "");
        }
        spEd.commit();
    }

    private void LoadUserDate() {
        SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);
        if (sp.getBoolean("isSave", false)) {
            String username = sp.getString("name", "");
            String userpassword = sp.getString("password", "");
            if (!("".equals(username) && "".equals(userpassword))) {
                CellPhoneNumber.setText(username);
                LoginPassword.setText(userpassword);
                RememberPassword.setChecked(true);
            }
        }
    }

    public void showCustomize() {
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(LoginActivity.this);
        final View dialogView = LayoutInflater.from(LoginActivity.this)
                .inflate(R.layout.dialog_dianhua, null);
        TextView textView = (TextView) dialogView.findViewById(R.id.edit_text);
        textView.setText("您的账号在其他地方登录");
        customizeDialog.setTitle("温馨提示");
        customizeDialog.setView(dialogView);
        customizeDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        customizeDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        customizeDialog.show();
    }
}
